package com.cicadasmall.system.wrapper;


import com.cicadasmall.common.constant.Constant;
import com.cicadasmall.common.func.Fn;
import com.cicadasmall.common.utils.SpringContextUtils;
import com.cicadasmall.common.utils.TreeUtils;
import com.cicadasmall.common.utils.WarpsUtils;
import com.cicadasmall.common.wrapper.EntityWrapper;
import com.cicadasmall.data.domain.AreaDO;
import com.cicadasmall.system.service.IAreaService;
import com.cicadasmall.system.vo.AreaVO;

import java.util.List;

public class AreaWrapper implements EntityWrapper<AreaDO, AreaVO> {

    private final static IAreaService areaService;

    static {
        areaService = SpringContextUtils.getBean(IAreaService.class);
    }

    public static AreaWrapper newBuilder() {
        return new AreaWrapper();
    }

    @Override
    public AreaVO entityVO(AreaDO entity) {
        AreaVO entityVo = WarpsUtils.copyTo(entity, AreaVO.class);
        if (Fn.isNotNull(entityVo.getParentId()) && Fn.notEqual(Constant.PARENT_ID, entityVo.getParentId())) {
            AreaDO parentArea = areaService.getById(entityVo.getParentId());
            if (Fn.isNotNull(parentArea)) {
                entityVo.setParentName(parentArea.getName());
            }
        } else {
            entityVo.setParentName("中国");
        }
        entityVo.setLeaf(Fn.equal(entityVo.getLevelType(), 3));
        entityVo.setMergerName(entityVo.getMergerName().replace(",", "/"));
        return entityVo;
    }


    @Override
    public List<AreaVO> treeVO(List<AreaDO> list) {
        return TreeUtils.toTree(listVO(list));
    }
}